package com.main;

import com.main.model.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * @author Zhang Guangyong
 * @desacription 主测试类
 * @date 2019年07月04日 16:16
 */
public class Main {


    public static void main(String[] args) {

        //准备
        Student student = new Student();
        Class<? extends Student> aClass = student.getClass();

        //验证 getFields()
        Field[] fields = aClass.getFields();
        System.out.println("输出 fields:");
        for (Field field : fields) {
            System.out.println(field);
        }

        //验证 getDeclaredFields()
        try {
            Field field = aClass.getDeclaredField("id");
            field.setAccessible(true);
            field.set(student, "测试id赋值");
            System.out.println(student.getId());

            //二次赋值
            for (Field f : aClass.getDeclaredFields()) {
                f.setAccessible(true);
                f.set(student, "测试赋值");
            }
            System.out.println("循环赋值测试 " + student.toString());

        } catch (IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("输出安全权限异常");
        } catch (NoSuchFieldException e) {
            System.out.println("输出没找到该字段异常");
        }
        System.out.println("输出 declaredFields 结束");

        //获取构造方法 getDeclaredConstructors()
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor<?> constructor : declaredConstructors) {
            System.out.println(constructor);
        }


    }

}
